/*
参数说明：
alertShow-弹出框参数{
  showmsg-服务器弹窗提示
  msg-服务器提示内容
}
components-商品组件信息{
  checklist-积分兑换商品List数组
  isMoney-是否显示金钱占位
  isScore-是否显示积分占位
  isRemove-是否显示购买按钮占位
  divsize-组件一排显示几个数据
  biaoqian-跳转积分详情还是商品详情界面(1 积分兑换界面  2 商品详情界面)
}
*/
<template>
  <div>
    <alert v-model="alertShow.showmsg" :title="'提示'" @on-show="onShow" @on-hide="onHide">{{alertShow.msg}}</alert>
    <div>
      <swiper :list="bannerList" :height="imgHw" dots-position="center" auto loop></swiper>
    </div>
    <div style="width: 100%;">
      <flexbox :gutter="0" style="padding-bottom: 20px;background-color: #ffffff">
        <flexbox-item :span="3.5">
          <div style="width: 80px;padding-left: 5px;">
            <img :src="LOGO" alt="LOGO" style="width: 80px;height: 80px;">
          </div>
        </flexbox-item>
        <flexbox-item>
          <table>
            <tr>
              <p style="font-size: 13px;word-break:break-all;padding-right: 10px;overflow: hidden;text-overflow: ellipsis;">{{subject.desc}}</p>
            </tr>
          </table>
        </flexbox-item>
      </flexbox>
      <shopGood :isList="components.checklist" :isMoney="components.isMoney" :isScore="components.isScore" :isRemove="components.isRemove " :divsize="components.size" :biaoqian="components.biaoqian"></shopGood>
      <div style="padding-bottom: 60px"></div>
    </div>
  </div>
</template>
<script>
  import { Flexbox, FlexboxItem, XButton, Alert, Swiper } from 'vux'
  import shopGood from '../../../components/shopCommodity/shopGoods_Q.vue'
  import * as urls from '../../../api/urls'
  import {API_ROOT} from '../../../config.js'
  import $ from 'jquery'

  export default {
    components: {
      Swiper,
      Flexbox,
      FlexboxItem,
      shopGood,
      XButton,
      Alert
    },
    mounted: function () {
      let width = document.body.clientWidth
      let heigth = (parseInt(width) / 100) * 44
      this.imgHw = heigth + 'px'
      this.id = this.$route.query.id
      this.getLogo()
      if (window.sessionStorage.getItem('userId') === '' || window.sessionStorage.getItem('userId') === null || window.sessionStorage.getItem('userId') === undefined) {
        let userId = window.location.href.replace(/.*userId=(\w*)&?.*/, '$1')
        let id = window.location.href.replace(/.*id=(\w*)&?.*/, '$1')
        this.userId = userId
        this.id = id
        if (this.userId !== '' && this.userId !== null && this.userId !== undefined) {
          window.sessionStorage.setItem('userId', this.userId)
          this.getList()
        } else {
          let url = window.location.origin
          window.location.href = url + '/api/wxAuth/index?type=t1'
        }
      } else {
        this.getList()
      }
    },
    data () {
      return {
        id: '',
        imgURl: '',
        LOGO: '',
        imgHw: '',
        alertShow: {
          showmsg: false,
          msg: ''
        },
        subject: {},
        bannerList: [],
        components: {
          checklist: [],
          isMoney: true,
          isRemove: true,
          isScore: false,
          size: 6,
          biaoqian: 2
        }
      }
    },
    methods: {
      getLogo () {
        // 获取logo
        let that = this
        this.http(urls.getLogo, {}, function (res) {
          if (res.status === 100) {
            if (res.data !== null) {
              that.LOGO = res.data.logo
              window.sessionStorage.setItem('LOGO', res.data.logo)
            }
          }
        }, function (res) {
        })
      },
      getList () {
        let that = this
        this.http(urls.subjectDetail, {id: that.id}, function (res) {
          if (res.status === 100) {
            if (res.data.subject !== null) {
              that.subject = res.data.subject
              for (let i = 0; i < that.subject.url.length; i++) {
                let bannerObj = {}
                bannerObj.img = that.subject.url[i]
                that.bannerList.push(bannerObj)
              }
            }
            if (res.data.items.length !== 0) {
              for (let i = 0; i < res.data.items.length; i++) {
                let itemObj = {}
                itemObj.url = res.data.items[i].itemPic
                itemObj.name = res.data.items[i].itemName
                itemObj.shopId = res.data.items[i].itemId
                itemObj.nowPrice = res.data.items[i].nowPrice
                itemObj.oldPrice = res.data.items[i].oldPrice
                that.components.checklist.push(itemObj)
              }
            }
            that.diyweixinfn2()
          } else {
            that.alertShow.showmsg = true
            that.alertShow.msg = res.errMsg
          }
        }, function (res) {
          that.alertShow.showmsg = true
          that.alertShow.msg = res
        })
      },
      onShow () {
        // 弹窗出现时触发
      },
      onHide () {
        // 弹窗关闭时触发
      },
      diyweixinfn2 () {
        let that = this
        let userId = window.sessionStorage.getItem('userId')
        let logo = window.sessionStorage.getItem('LOGO')
        let url = window.location.origin
        let link = url + '/api/wxAuth/index?type=t8&id=' + that.id + '&uid=' + userId
        that.$wechat.ready(function () {
          that.$wechat.onMenuShareAppMessage({
            title: that.subject.name, // 分享标题
            desc: that.subject.desc, // 分享描述
            link: link, // 分享链接
//            imgUrl: that.subject.url[0], // 分享图标
            imgUrl: logo, // 分享图标
            success: function () {
              // 用户确认分享后执行的回调函数
            },
            cancel: function () {
              // 用户取消分享后执行的回调函数
            }
          })
          that.$wechat.onMenuShareTimeline({
            title: that.subject.name, // 分享标题
            link: link, // 分享链接
            imgUrl: logo, // 分享图标
            success: function () {
              // 用户确认分享后执行的回调函数
            },
            cancel: function () {
              // 用户取消分享后执行的回调函数
            }
          })
        })
        $.ajax({
          url: API_ROOT + '/api/wxAuth/jsauth?url=' + encodeURIComponent(url + '/static/index.html'),
          type: 'post',
          success: function (t) {
            if (t.status === 100) {
              let sign = t.data.sign
              let weixinConfigdiyweixinfn2 = {
                debug: false,
                appId: sign.appId,
                timestamp: sign.timeStamp,
                nonceStr: sign.nonceStr,
                signature: sign.signature,
                jsApiList: ['onMenuShareAppMessage', 'chooseWXPay', 'onMenuShareTimeline']
              }
              // 授权
              that.$wechat.config(weixinConfigdiyweixinfn2)
              // 调用ready初始化
            } else {
              that.diyweixinfn2()
            }
          },
          error: function (t) {
            that.diyweixinfn2()
          }
        })
      }
    }
  }
</script>
<style scoped="scoped">
</style>
